package com.controller;

import com.bean.dangBean.Consumer;
import com.service.ConsumerService;
import com.util.PageAssistant;
import com.vo.ConsumerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("con")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;

    @RequestMapping("query")
    public String query(Model model, HttpSession session, ConsumerVo conQueryInfo,
                        PageAssistant<Consumer> paConsumer){
        System.out.println(">>> con query");
        System.out.println(conQueryInfo);
        System.out.println(paConsumer);
        System.out.println("------------------------------------------------");

        //创建参数集合
        Map<String, Object> map = new HashMap<>();
        //添加查询参数
        map.put("key", conQueryInfo.getUserName());

        //查询总记录数
        paConsumer.setCount(consumerService.count(map));

        //设置分页参数
        map.put("start", paConsumer.getStart());
        map.put("size", paConsumer.getSize());


        //查询当前页内容
        paConsumer.setItems(consumerService.query(map));

        //保存
        model.addAttribute("paConsumer", paConsumer);
        model.addAttribute("conQueryInfo", conQueryInfo.parse());

        return "page/consumer/conlist";
    }

    @RequestMapping("unique")
    public String unqiue(Model model, HttpSession session, ConsumerVo con, String page){
        System.out.println(">>> con unique");
        System.out.println(con);
        System.out.println(page);
        System.out.println("------------------------------------------------");

        //查询
        List<Consumer> conList = consumerService.find(con.parse());

        //保存
        session.setAttribute("conInfo", conList.get(0));
        model.addAttribute("conInfo", conList.get(0));
        System.out.println("conList为"+conList.get(0));
        //判断, page是否为null
//        page = page == null ? "detail.jsp" : page;
        if (page == null){
            page = "conDetail.html";
        }

        return "page/consumer/" + page;
    }

    @RequestMapping("remove")
    public String remove(HttpSession session, Consumer consumer){
        System.out.println(">>> con remove");
        System.out.println(consumer);
        System.out.println("------------------------------------------------");

        //删除
        int count = consumerService.remove(consumer);

        //保存
        String updateMsg = "删除成功!";
        String updateClass = "update-suc";
        if(count != 1){
            updateMsg = "删除失败!";
            updateClass = "update-err";
        }
        session.setAttribute("updateMsg", updateMsg);
        session.setAttribute("updateClass", updateClass);

        return "redirect:/con/query";

    }

    @RequestMapping("modify")
    public String modify(HttpSession session, ConsumerVo consumerVo){
        System.out.println(">>> con modify");
        System.out.println(consumerVo);
        System.out.println("------------------------------------------------");

        //修改
        int count = consumerService.modify(consumerVo.parse());


        return "redirect:/con/unique?id="+consumerVo.getId();
    }

    @RequestMapping("save")
    public String save(HttpSession session, ConsumerVo consumerVo){
        System.out.println(">>> con add");
        System.out.println(consumerVo);
        System.out.println("------------------------------------------------");

        //添加
        int count = consumerService.save(consumerVo.parse());
        return "redirect:/con/unique?userName="+consumerVo.getUserName();
    }

}
